package com.varbal.icegame.config;

import com.varbal.icegame.component.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

//@EnableWebMvc//抛弃springboot的mvc配置，全部自己配置
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//    @Override
//    @Bean
//    public void addViewControllers(ViewControllerRegistry registry) {
////        super.addViewControllers(registry);
//        //浏览器发送hello请求，返回index页面
//        registry.addViewController("hello").setViewName("index");
//    }

//    注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("addInterceptors...registry...");
        registry.addInterceptor(new LoginHandlerInterceptor()).excludePathPatterns("/index.html","/","/hello");//不拦截的url，可以放静态资源
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        System.out.println("我是MyWebConfig跨域");
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }
//    @Bean
//    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
//        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
//            @Override
//            public void addInterceptors(InterceptorRegistry registry) {//    注册拦截器
//                super.addInterceptors(registry);
//                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
//                        .excludePathPatterns("/index.html","/","hello");
//            }
//
//            @Override
//            public void addViewControllers(ViewControllerRegistry registry) {
////                super.addViewControllers(registry);
//
//            }
//        };
//        return adapter;
//    }

}
